package com.archermind.boc.utility;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.archermind.boc.Logger;
import com.archermind.boc.dal.business.Icommision;
import com.archermind.boc.dal.business.support.DispatchSupport;
import com.archermind.boc.domain.Biz;
import com.archermind.boc.domain.IQ;
import com.archermind.boc.ehcache.UserCache;
import com.archermind.boc.ehcache.UserEhcacheImpl;

public class BizHelper {
	private Icommision commision;

	public Icommision getCommision() {
		return commision;
	}

	public void setCommision(Icommision commision) {
		this.commision = commision;
	}

	private Logger log = Logger.getLogger(BizHelper.class.getName());

	public void grant(UserCache userCache, IQ iq, Biz biz) {
		UserEhcacheImpl userEhcache = new UserEhcacheImpl();
		log.severe("------------分配授权端：userid:" + userCache.getUserid()
				+ "sessionid:" + userCache.getSessionID() + "--------");
		String jingbanSessionId = iq.getSessionID();
		log.severe("----------经办端sessionid:" + jingbanSessionId
				+ "------------");
		iq.setSessionID(userCache.getSessionID());
		System.out.println(iq.getSessionID());
		int timeoutSecond = commision.getTimeOut(biz.getBizTypeID());
		List<String> list = new ArrayList<String>();
		list.add(String.valueOf(timeoutSecond));
		iq.getSession().getX().put("sessionTimeOut", list);
		commision.write(iq);// 发送到授权端
		log.severe("发送到授权端iq: sessionid:" + iq.getSessionID() + "");
		DispatchSupport.setMessage(userCache, iq, jingbanSessionId, biz,
				timeoutSecond);

		System.out.println(iq.getSessionID());
		commision.write(iq);// 发送到经办端
		StringBuffer buf = new StringBuffer();
		for (Map.Entry e : iq.getSession().getX().entrySet()) {
			buf.append("[" + e.getKey() + ":" + e.getValue() + "]");
		}
		log.info("发送到经办端的iq数据:" + buf.toString());
		commision.modifyBiz(biz, userCache);
		log.info("更新biz表ToBankNum字段：bizid:" + biz.getBizid());
		userCache.setBiz(biz);
		commision.modifyUserCache(userCache);// 做特别处理，填充cache中biz对
		log.info("填充userCache中授权端的biz: bizid=" + biz.getBizid());
		UserCache jingbanuserCache = userEhcache.getUserCachebySessionId(iq
				.getSessionID());
		jingbanuserCache.setBiz(biz);
		commision.modifyUserCache(jingbanuserCache);// 做特别处理，填充cache中biz对
		log.info("填充userCache中经办端的biz: bizid=" + biz.getBizid() + " userid="
				+ jingbanuserCache.getUserid());
	}
}
